layui.use(['form'], function(){
    var form = layui.form;

    $(function() {
        form.on('submit(formDemo)', function(data) {
            var answerList = new Array()
                ,s = 0;
            $.each( data.field , function(i, n){
                var index =i.indexOf("\_")
                    ,qid = i.substring(index + 1,i.length)
                    ,prefix = i.substring(0,index + 1)
                    ,score = $("." + i).val();
                if (prefix == "multiple_"){
                    var arr = new Array();
                    $("input:checkbox[name=" + i + "]:checked").each(function(x){
                        arr[x] = $(this).val();
                    });
                    data.field[i] = arr.join(",");
                }
                if (prefix == "short_"){
                    s += parseInt(score);
                }
                answerList.push({
                    id: qid,
                    answer: data.field[i],
                    score: score
                });
            });

            //交卷参数  answerList
            $.ajax({
                url: "/userPaper/reviewPaper",
                type: "post",
                async: false,
                data:{
                    answers: JSON.stringify(answerList),
                    upid: $(".upId").val(),
                    aid: $(".aId").val(),
                    score: s
                },
                success:function (result) {
                    if (result.code == 200) {
                        layer.tips(result.msg, '#butt', {
                            tips: [1, '#3595CC'],
                            time: 1500
                        });
                        $("#butt").attr("disabled","disabled");
                        setTimeout(function () {
                            window.location.href='/markingList';
                        }, 1500);

                    } else {
                        layer.tips(result.msg, '#butt', {
                            tips: [1, '#FF5722'],
                            time: 1500});
                    }
                }
            });
            return false;
        });
    });
});